package com.zhaodui.order.controller;
import com.zhaodui.loan.entity.FxjLoanMaEntity;
import com.zhaodui.order.entity.*;
import com.zhaodui.order.service.FxjOrderMainServiceI;
import com.zhaodui.order.page.FxjOrderMainPage;

import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhaodui.pro.entity.*;
import com.zhaodui.sys.entity.FxjOtherLoginEntity;
import com.zhaodui.util.fxjUtil;
import com.zhaodui.wf.entity.FxjSysWfEntity;
import io.swagger.models.auth.In;
import org.apache.log4j.Logger;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.TSUserOrg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.common.TreeChildCount;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import java.io.OutputStream;
import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jeecgframework.core.util.ResourceUtil;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.core.util.ExceptionUtil;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.jwt.util.GsonUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import static com.xiaoleilu.hutool.date.DateTime.now;

/**   
 * @Title: Controller
 * @Description: 订单
 * @author onlineGenerator
 * @date 2018-11-30 14:39:23
 * @version V1.0   
 *
 */
@Api(value="FxjOrderMain",description="订单",tags="fxjOrderMainController")
@Controller
@RequestMapping("/fxjOrderMainController")
public class FxjOrderMainController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FxjOrderMainController.class);

	@Autowired
	private FxjOrderMainServiceI fxjOrderMainService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;

	private  String proNo;
	private  String proName;
	/**
	 * 订单列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList");
	}

	@RequestMapping(params = "list1")
	public ModelAndView list1(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList1");
	}

	@RequestMapping(params = "list10")
	public ModelAndView list10(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList10");
	}

	@RequestMapping(params = "list20")
	public ModelAndView list20(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList20");
	}
	@RequestMapping(params = "list30")
	public ModelAndView list30(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList30");
	}
	@RequestMapping(params = "list40")
	public ModelAndView list40(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList40");
	}
	@RequestMapping(params = "list50")
	public ModelAndView list50(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList50");
	}
	@RequestMapping(params = "list60")
	public ModelAndView list60(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList60");
	}
	@RequestMapping(params = "list70")
	public ModelAndView list70(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList70");
	}
	@RequestMapping(params = "list80")
	public ModelAndView list80(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList80");
	}
	@RequestMapping(params = "list90")
	public ModelAndView list90(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/order/fxjOrderMainList90");
	}


	/**
	 *
	 *
	 * @return
	 */
	@RequestMapping(params = "setvalue")
	@ResponseBody
	public AjaxJson setvalue(String proNoi,HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		proNo = proNoi;
		return j;
	}
	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(FxjOrderMainEntity fxjOrderMain,HttpServletRequest request,String bpmStstus, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(FxjOrderMainEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, fxjOrderMain,request.getParameterMap());
		try{
		//自定义追加查询条件

             TSUser tsUser  = ResourceUtil.getSessionUser();
             String orgcode = tsUser.getCurrentDepart().getOrgCode();
             cq.like("sysOrgCode",orgcode+"%");
			if(StringUtil.isNotEmpty(bpmStstus)){
				cq.eq("bpmStatus",bpmStstus);
			}
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.fxjOrderMainService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除订单
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(FxjOrderMainEntity fxjOrderMain, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		fxjOrderMain = systemService.getEntity(FxjOrderMainEntity.class, fxjOrderMain.getId());
		String message = "订单删除成功";
		try{
			fxjOrderMainService.delMain(fxjOrderMain);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "订单删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 批量删除订单
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		String message = "订单删除成功";
		try{
			for(String id:ids.split(",")){
				FxjOrderMainEntity fxjOrderMain = systemService.getEntity(FxjOrderMainEntity.class,
				Integer.parseInt(id)
				);
				fxjOrderMainService.delMain(fxjOrderMain);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "订单删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 添加订单
	 * 
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(HttpServletRequest request,FxjOrderMainPage fxjOrderMainPage) {
		AjaxJson j = new AjaxJson();
		String message = "添加成功";
		try {
			fxjOrderMainService.addMain(fxjOrderMainPage);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "订单添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	/**
	 * 更新订单
	 * 
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(HttpServletRequest request,String esContent,FxjOrderMainPage fxjOrderMainPage) {
		AjaxJson j = new AjaxJson();
		String message = "更新成功";
		try{

			List<FxjOrderLogEntity> fxjOrderLogList = fxjOrderMainPage.getFxjOrderLogList();
			FxjOrderLogEntity fxjOrderLogEntity = new FxjOrderLogEntity();
			fxjOrderLogEntity.setOrderNo(fxjOrderMainPage.getOrderNo());
			fxjOrderLogEntity.setLogUser(ResourceUtil.getSessionUser().getUserName());
			fxjOrderLogEntity.setLogDate(now());
			fxjOrderLogList.add(fxjOrderLogEntity);
			fxjOrderLogEntity.setLogContent(esContent);//审批意见
			fxjOrderMainPage.setFxjOrderLogList(fxjOrderLogList);
			fxjOrderMainService.updateMain(fxjOrderMainPage);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "更新订单失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 订单新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(String proNoa,HttpServletRequest req) {

		FxjOrderMainPage fxjOrderMainPage = new FxjOrderMainPage();
		String pronnoadd="";
		if(StringUtil.isNotEmpty(proNo)){
			pronnoadd = proNo;
		}
		if(StringUtil.isNotEmpty(proNoa)){
			pronnoadd = ResourceUtil.getConfigByName("fxj.defprono");
		}
		if (StringUtil.isNotEmpty(pronnoadd)) {
			FxjOrderMainEntity fxjOrderMain = new FxjOrderMainEntity();
			// 根据proNO查找
			FxjProMainEntity fxjProMainEntity = systemService.findUniqueByProperty(FxjProMainEntity.class,"proNo",pronnoadd);
			if(fxjProMainEntity!=null){
				fxjOrderMain.setProNo(fxjProMainEntity.getProNo());
				fxjOrderMain.setProName(fxjProMainEntity.getProName());
				fxjOrderMain.setProClass(fxjProMainEntity.getProClass());
				fxjOrderMain.setProType(fxjProMainEntity.getProType());
				TSUser tsUser = ResourceUtil.getSessionUser();
				fxjOrderMain.setCreateUser(tsUser.getUserName());
				fxjOrderMain.setCreateSysorg(tsUser.getCurrentDepart().getOrgCode());
			}
			try {
				MyBeanUtils.copyBeanNotNull2Bean(fxjOrderMain,fxjOrderMainPage);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//===================================================================================
			//获取参数
			Object proNo3 = pronnoadd;
			//===================================================================================
			//查询-费用
			String hql3 = "from FxjProCostEntity where 1 = 1 AND proNo = ? ";
			List<FxjProCostEntity> fxjProCostEntityList = systemService.findHql(hql3,proNo3);
			if(fxjProCostEntityList==null || fxjProCostEntityList.size()<=0){
				fxjProCostEntityList = new ArrayList<FxjProCostEntity>();
				fxjProCostEntityList.add(new FxjProCostEntity());
			}
			//===================================================================================
			//获取参数
			Object proNo4 = pronnoadd;
			//===================================================================================
			//查询-其他计算
			String hql4 = "from FxjProCostCountEntity where 1 = 1 AND proNo = ? ";
			List<FxjProCostCountEntity> fxjProCostCountEntityList = systemService.findHql(hql4,proNo4);
			if(fxjProCostCountEntityList==null || fxjProCostCountEntityList.size()<=0){
				fxjProCostCountEntityList = new ArrayList<FxjProCostCountEntity>();
				fxjProCostCountEntityList.add(new FxjProCostCountEntity());
			}
			//===========页面需要展示的信息========================================================================
			//查询-产品信息
			List<FxjOrderObjEntity> fxjOrderObjEntityList =  new ArrayList<FxjOrderObjEntity>();
			if(fxjOrderObjEntityList==null || fxjOrderObjEntityList.size()<=0){
				fxjOrderObjEntityList = new ArrayList<FxjOrderObjEntity>();
				fxjOrderObjEntityList.add(new FxjOrderObjEntity());
			}
			fxjOrderMainPage.setFxjOrderObjList(fxjOrderObjEntityList);
			//===================================================================================
			//===================================================================================
			//查询-附件
			//获取参数
			Object proNo0 = pronnoadd;
			//===================================================================================
			//查询-附件
			String hql0 = "from FxjProAttEntity where 1 = 1 AND proNo = ? ";
			List<FxjProAttEntity> fxjProAttEntityList = systemService.findHql(hql0,proNo0);
			List<FxjOrderAttaEntity> fxjOrderAttaEntityList = new ArrayList<FxjOrderAttaEntity>();
			for(FxjProAttEntity t:fxjProAttEntityList){
				FxjOrderAttaEntity  fxjOrderAttaEntity = new FxjOrderAttaEntity();
				fxjOrderAttaEntity.setAttaType(t.getAttType());
				fxjOrderAttaEntity.setAttaName(t.getAttName());
				fxjOrderAttaEntityList.add(fxjOrderAttaEntity);
			}
			fxjOrderMainPage.setFxjOrderAttaList(fxjOrderAttaEntityList);
			//===================================================================================
		}
		req.setAttribute("fxjOrderMainPage", fxjOrderMainPage);

		return new ModelAndView("com/zhaodui/order/fxjOrderMain-add");
	}
	
	/**
	 * 订单编辑页面跳转
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(HttpServletRequest req) {
		String id = req.getParameter("id");
		FxjOrderMainPage fxjOrderMainPage = new FxjOrderMainPage();
		if (StringUtil.isNotEmpty(id)) {
			FxjOrderMainEntity fxjOrderMain = fxjOrderMainService.getEntity(FxjOrderMainEntity.class,Integer.parseInt(id));
			try {
				MyBeanUtils.copyBeanNotNull2Bean(fxjOrderMain,fxjOrderMainPage);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//===================================================================================
			//获取参数
			Object orderNo0 = fxjOrderMain.getOrderNo();
			//===================================================================================
		    //查询-日志
		    String hql0 = "from FxjOrderLogEntity where 1 = 1 AND orderNo = ? ";
		    List<FxjOrderLogEntity> fxjOrderLogEntityList = systemService.findHql(hql0,orderNo0);
		    if(fxjOrderLogEntityList==null || fxjOrderLogEntityList.size()<=0){
		    	fxjOrderLogEntityList = new ArrayList<FxjOrderLogEntity>();
		    	fxjOrderLogEntityList.add(new FxjOrderLogEntity());
		    }
		    fxjOrderMainPage.setFxjOrderLogList(fxjOrderLogEntityList);
			//===================================================================================
			//获取参数
			Object orderNo1 = fxjOrderMain.getOrderNo();
			//===================================================================================
		    //查询-产品信息
		    String hql1 = "from FxjOrderObjEntity where 1 = 1 AND orderNo = ? ";
		    List<FxjOrderObjEntity> fxjOrderObjEntityList = systemService.findHql(hql1,orderNo1);
		    if(fxjOrderObjEntityList==null || fxjOrderObjEntityList.size()<=0){
		    	fxjOrderObjEntityList = new ArrayList<FxjOrderObjEntity>();
		    	fxjOrderObjEntityList.add(new FxjOrderObjEntity());
		    }
		    fxjOrderMainPage.setFxjOrderObjList(fxjOrderObjEntityList);
			//===================================================================================
			//获取参数
			Object orderNo2 = fxjOrderMain.getOrderNo();
			//===================================================================================
		    //查询-融资物
		    String hql2 = "from FxjOrderLoanEntity where 1 = 1 AND orderNo = ? ";
		    List<FxjOrderLoanEntity> fxjOrderLoanEntityList = systemService.findHql(hql2,orderNo2);
		    if(fxjOrderLoanEntityList==null || fxjOrderLoanEntityList.size()<=0){
		    	fxjOrderLoanEntityList = new ArrayList<FxjOrderLoanEntity>();
		    	fxjOrderLoanEntityList.add(new FxjOrderLoanEntity());
		    }
		    fxjOrderMainPage.setFxjOrderLoanList(fxjOrderLoanEntityList);
			//===================================================================================
			//获取参数
			Object orderNo3 = fxjOrderMain.getOrderNo();
			//===================================================================================
		    //查询-紧急联系人
		    String hql3 = "from FxjOrderEmerEntity where 1 = 1 AND orderNo = ? ";
		    List<FxjOrderEmerEntity> fxjOrderEmerEntityList = systemService.findHql(hql3,orderNo3);
		    if(fxjOrderEmerEntityList==null || fxjOrderEmerEntityList.size()<=0){
		    	fxjOrderEmerEntityList = new ArrayList<FxjOrderEmerEntity>();
		    	fxjOrderEmerEntityList.add(new FxjOrderEmerEntity());
		    }
		    fxjOrderMainPage.setFxjOrderEmerList(fxjOrderEmerEntityList);
			//===================================================================================
			//获取参数
			Object orderNo4 = fxjOrderMain.getOrderNo();
			//===================================================================================
		    //查询-收还款
		    String hql4 = "from FxjOrderDueEntity where 1 = 1 AND orderNo = ? ";
		    List<FxjOrderDueEntity> fxjOrderDueEntityList = systemService.findHql(hql4,orderNo4);
		    if(fxjOrderDueEntityList==null || fxjOrderDueEntityList.size()<=0){
		    	fxjOrderDueEntityList = new ArrayList<FxjOrderDueEntity>();
		    	fxjOrderDueEntityList.add(new FxjOrderDueEntity());
		    }
		    fxjOrderMainPage.setFxjOrderDueList(fxjOrderDueEntityList);
			//===================================================================================
			//获取参数
			Object orderNo5 = fxjOrderMain.getOrderNo();
			//===================================================================================
		    //查询-附件
		    String hql5 = "from FxjOrderAttaEntity where 1 = 1 AND orderNo = ? ";
		    List<FxjOrderAttaEntity> fxjOrderAttaEntityList = systemService.findHql(hql5,orderNo5);
		    if(fxjOrderAttaEntityList==null || fxjOrderAttaEntityList.size()<=0){
		    	fxjOrderAttaEntityList = new ArrayList<FxjOrderAttaEntity>();
		    	fxjOrderAttaEntityList.add(new FxjOrderAttaEntity());
		    }
		    fxjOrderMainPage.setFxjOrderAttaList(fxjOrderAttaEntityList);
			//===================================================================================
			//获取参数
			Object orderNo6 = fxjOrderMain.getOrderNo();
			//===================================================================================
		    //查询-风控
		    String hql6 = "from FxjOrderRmEntity where 1 = 1 AND orderNo = ? ";
		    List<FxjOrderRmEntity> fxjOrderRmEntityList = systemService.findHql(hql6,orderNo6);
		    if(fxjOrderRmEntityList==null || fxjOrderRmEntityList.size()<=0){
		    	fxjOrderRmEntityList = new ArrayList<FxjOrderRmEntity>();
		    	fxjOrderRmEntityList.add(new FxjOrderRmEntity());
		    }
		    fxjOrderMainPage.setFxjOrderRmList(fxjOrderRmEntityList);
		}
		req.setAttribute("fxjOrderMainPage", fxjOrderMainPage);
		return new ModelAndView("com/zhaodui/order/fxjOrderMain-update");
	}
	
    /**
    * 导出excel
    *
    * @param request
    * @param response
    */
    @RequestMapping(params = "exportXls")
    public String exportXls(FxjOrderMainEntity fxjOrderMain,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid,ModelMap map) {
    	CriteriaQuery cq = new CriteriaQuery(FxjOrderMainEntity.class, dataGrid);
    	//查询条件组装器
    	org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, fxjOrderMain);
    	try{
    	//自定义追加查询条件
    	}catch (Exception e) {
    		throw new BusinessException(e.getMessage());
    	}
    	cq.add();
    	List<FxjOrderMainEntity> list=this.fxjOrderMainService.getListByCriteriaQuery(cq, false);
    	List<FxjOrderMainPage> pageList=new ArrayList<FxjOrderMainPage>();
        if(list!=null&&list.size()>0){
        	for(FxjOrderMainEntity entity:list){
        		try{
        			FxjOrderMainPage page=new FxjOrderMainPage();
        			MyBeanUtils.copyBeanNotNull2Bean(entity,page);
           			Object orderNo0 = entity.getOrderNo();
					String hql0 = "from FxjOrderLogEntity where 1 = 1 AND orderNo = ? ";
					List<FxjOrderLogEntity> fxjOrderLogEntityList = systemService.findHql(hql0,orderNo0);
					page.setFxjOrderLogList(fxjOrderLogEntityList);
           			Object orderNo1 = entity.getOrderNo();
					String hql1 = "from FxjOrderObjEntity where 1 = 1 AND orderNo = ? ";
					List<FxjOrderObjEntity> fxjOrderObjEntityList = systemService.findHql(hql1,orderNo1);
					page.setFxjOrderObjList(fxjOrderObjEntityList);
           			Object orderNo2 = entity.getOrderNo();
					String hql2 = "from FxjOrderLoanEntity where 1 = 1 AND orderNo = ? ";
					List<FxjOrderLoanEntity> fxjOrderLoanEntityList = systemService.findHql(hql2,orderNo2);
					page.setFxjOrderLoanList(fxjOrderLoanEntityList);
           			Object orderNo3 = entity.getOrderNo();
					String hql3 = "from FxjOrderEmerEntity where 1 = 1 AND orderNo = ? ";
					List<FxjOrderEmerEntity> fxjOrderEmerEntityList = systemService.findHql(hql3,orderNo3);
					page.setFxjOrderEmerList(fxjOrderEmerEntityList);
           			Object orderNo4 = entity.getOrderNo();
					String hql4 = "from FxjOrderDueEntity where 1 = 1 AND orderNo = ? ";
					List<FxjOrderDueEntity> fxjOrderDueEntityList = systemService.findHql(hql4,orderNo4);
					page.setFxjOrderDueList(fxjOrderDueEntityList);
           			Object orderNo5 = entity.getOrderNo();
					String hql5 = "from FxjOrderAttaEntity where 1 = 1 AND orderNo = ? ";
					List<FxjOrderAttaEntity> fxjOrderAttaEntityList = systemService.findHql(hql5,orderNo5);
					page.setFxjOrderAttaList(fxjOrderAttaEntityList);
           			Object orderNo6 = entity.getOrderNo();
					String hql6 = "from FxjOrderRmEntity where 1 = 1 AND orderNo = ? ";
					List<FxjOrderRmEntity> fxjOrderRmEntityList = systemService.findHql(hql6,orderNo6);
					page.setFxjOrderRmList(fxjOrderRmEntityList);
            		pageList.add(page);
            	}catch(Exception e){
            		logger.info(e.getMessage());
            	}
            }
        }
        map.put(NormalExcelConstants.FILE_NAME,"订单");
        map.put(NormalExcelConstants.CLASS,FxjOrderMainPage.class);
        map.put(NormalExcelConstants.PARAMS,new ExportParams("订单列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(), "导出信息"));
        map.put(NormalExcelConstants.DATA_LIST,pageList);
        return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}

    /**
	 * 通过excel导入数据
	 * @param request
	 * @param
	 * @return
	 */
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(2);
			params.setNeedSave(true);
			try {
				List<FxjOrderMainPage> list =  ExcelImportUtil.importExcel(file.getInputStream(), FxjOrderMainPage.class, params);
				for (FxjOrderMainPage page : list) {
		            fxjOrderMainService.addMain(page);
				}
				j.setMsg("文件导入成功！");
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				logger.error(ExceptionUtil.getExceptionMessage(e));
			}finally{
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			}
			return j;
	}
	/**
	* 导出excel 使模板
	*/
	@RequestMapping(params = "exportXlsByT")
	public String exportXlsByT(ModelMap map) {
		map.put(NormalExcelConstants.FILE_NAME,"订单");
		map.put(NormalExcelConstants.CLASS,FxjOrderMainPage.class);
		map.put(NormalExcelConstants.PARAMS,new ExportParams("订单列表", "导出人:"+ ResourceUtil.getSessionUser().getRealName(),
		"导出信息"));
		map.put(NormalExcelConstants.DATA_LIST,new ArrayList());
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	/**
	* 导入功能跳转
	*
	* @return
	*/
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest req) {
		req.setAttribute("controller_name", "fxjOrderMainController");
		return new ModelAndView("common/upload/pub_excel_upload");
	}
	@RequestMapping(value="/filelist/{orderNo}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="订单file信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjOrderAttaEntity>> filelist(@PathVariable("orderNo") String orderNo, @RequestParam(value="attaType", required=false) String attaType, HttpServletRequest request) {
		FxjLoanMaEntity task = null;
		if("ma".equals(attaType)){
			try{
				task = fxjOrderMainService.get(FxjLoanMaEntity.class, Integer.parseInt(orderNo));

			}catch (Exception e){

			}
		}



		if(task!=null){
			orderNo = task.getOrderNo();
		}
		CriteriaQuery query = new CriteriaQuery(FxjOrderMainEntity.class);


		query.add();



		Map<String,Object> map = new HashMap<String,Object>();
		map.put("logDate", "desc");
		query.setOrder(map);

		String hql0 = "from FxjOrderAttaEntity where 1 = 1 AND orderNo = ? ";
		if(StringUtil.isNotEmpty(attaType)){
			hql0 = "from FxjOrderAttaEntity where 1 = 1 AND orderNo = ? and attaType = '" +attaType+"'";
		}
		List<FxjOrderAttaEntity> fxjOrderfileList = this.fxjOrderMainService.findHql(hql0,orderNo);

		return Result.success(fxjOrderfileList);
	}
	@RequestMapping(value="/rmlist/{orderNo}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="订单风控信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjOrderRmEntity>> rmlist(@PathVariable("orderNo") String orderNo, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjOrderMainEntity.class);


		query.add();

		query.setCurPage(pageNumber<=0?1:pageNumber);
		query.setPageSize(pageSize<1?1:pageSize);
		query.add();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("logDate", "desc");
		query.setOrder(map);
		String hql0 = "from FxjOrderRmEntity where 1 = 1 AND orderNo = ? ";
		List<FxjOrderRmEntity> fxjOrderrmList = this.fxjOrderMainService.findHql(hql0,orderNo);

		return Result.success(fxjOrderrmList,(long)fxjOrderrmList.size());
	}

	@RequestMapping(value="/attalist/{orderNo}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="订单附件信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjOrderAttaEntity>> attalist(@RequestParam(value="attaType", required=false)  String attaType, @PathVariable("orderNo") String orderNo, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjOrderMainEntity.class);


		query.add();

		query.setCurPage(pageNumber<=0?1:pageNumber);
		query.setPageSize(pageSize<1?1:pageSize);
		query.add();
		Map<String,Object> map = new HashMap<String,Object>();
		query.setOrder(map);

		String hql0 = "from FxjOrderAttaEntity where 1 = 1  AND bpmStatus = 1 AND orderNo = ? ";
		if(StringUtil.isNotEmpty(attaType)){
			hql0 = "from FxjOrderAttaEntity where 1 = 1  AND bpmStatus = 1 AND orderNo = ? and attaType = '"+attaType+"'";
		}
		List<FxjOrderAttaEntity> fxjOrderattaList = this.fxjOrderMainService.findHql(hql0,orderNo);

		return Result.success(fxjOrderattaList,(long)fxjOrderattaList.size());
	}
	@RequestMapping(value="/loglist/{orderNo}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="订单日志信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjOrderLogEntity>> loglist(@PathVariable("orderNo") String orderNo, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjOrderMainEntity.class);


		query.add();

		query.setCurPage(pageNumber<=0?1:pageNumber);
		query.setPageSize(pageSize<1?1:pageSize);
		query.add();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("logDate", "desc");
		query.setOrder(map);
		String hql0 = "from FxjOrderLogEntity where 1 = 1 AND orderNo = ? ";
		List<FxjOrderLogEntity> fxjOrderLogOldList = this.fxjOrderMainService.findHql(hql0,orderNo);

		return Result.success(fxjOrderLogOldList,(long)fxjOrderLogOldList.size());
	}
 	@RequestMapping(value="/list/{username}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="订单列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjOrderMainPage>> list(@PathVariable("username") String username, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjOrderMainEntity.class);


        try{

			if(StringUtil.isNotEmpty(request.getParameter("orderNo"))){
				query.like("orderNo","%"+request.getParameter("orderNo")+"%");
			}
        	if(StringUtil.isNotEmpty(request.getParameter("cusName"))){
				query.like("cusName","%"+request.getParameter("cusName")+"%");
			}
			if(StringUtil.isNotEmpty(request.getParameter("cusMobile"))){
				query.like("cusMobile","%"+request.getParameter("cusMobile")+"%");
			}
			if(StringUtil.isNotEmpty(request.getParameter("cusIncard"))){
				query.like("cusIncard","%"+request.getParameter("cusIncard")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("orderStatusType"))){
	            if("0".equals(request.getParameter("orderStatusType"))){
					String[] bpmStatus = new String[]{"1","10","20","25","30","40","50","60","70"};

					query.in("bpmStatus",bpmStatus);
				}
				if("1".equals(request.getParameter("orderStatusType"))){
					String[] bpmStatus = new String[]{"80","99","100"};

					query.in("bpmStatus",bpmStatus);
				}
				if("3".equals(request.getParameter("orderStatusType"))){
					String[] bpmStatus = new String[]{"1"};

					query.in("bpmStatus",bpmStatus);
				}
			}else{
				if(StringUtil.isNotEmpty(request.getParameter("bpmStatus"))){
					query.eq("bpmStatus",request.getParameter("bpmStatus"));
				}
			}


			String orgcode = "";
			System.out.print("user**********"+username);
			if (StringUtil.getStringLen(username)>20){

                    query.eq("createBy",username);
				System.out.print("createBy**********len"+username);


			}else{
				if(StringUtil.isNotEmpty(request.getParameter("listtype"))){//
					 if("fenpei".equals(request.getParameter("listtype"))){//已经分配
						query.eq("orderBy18",username);
					}
					if("weifenpei".equals(request.getParameter("listtype"))){
						query.eq("orderBy18","weifenpei");// 未分配清单
					}
				}
                TSUser task = fxjUtil.getsysorgcode(username);
                if(task!=null){
                    orgcode=task.getCurrentDepart().getSysOrgCode();

                }

			if(StringUtil.isNotEmpty(orgcode)){
			      query.like("sysOrgCode",orgcode+"%");
			} else{
                query.like("sysOrgCode","NO"+"%");

            }

			}




        }catch (Exception e){
        }
		query.add();
        List<FxjOrderMainEntity> listsize = this.fxjOrderMainService.getListByCriteriaQuery(query,false);

        query.setCurPage(pageNumber<=0?1:pageNumber);
        query.setPageSize(pageSize<1?1:pageSize);
        query.add();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("createDate", "desc");
		query.setOrder(map);
		List<FxjOrderMainEntity> list = this.fxjOrderMainService.getListByCriteriaQuery(query,true);

        List<FxjOrderMainPage> pageList=new ArrayList<FxjOrderMainPage>();
        if(list!=null&&list.size()>0){
        	for(FxjOrderMainEntity entity:list){
        		try{
        			FxjOrderMainPage page=new FxjOrderMainPage();

        		   MyBeanUtils.copyBeanNotNull2Bean(entity,page);

					FxjOtherLoginEntity fxjOtherLoginEntity = systemService.findUniqueByProperty(FxjOtherLoginEntity.class,"otherid",entity.getCreateBy());
					if(fxjOtherLoginEntity==null){
						page.setCreateBy("系统");
					}else{
						page.setCreateBy(fxjOtherLoginEntity.getUsername());
					}
//        		   page.setCusName(entity.getCusName()+"<br>"+entity.getProName()+"<br>"+entity.getCusMobile());
//					page.setCreateSysorg(entity.getCreateBy()+"<br>"+entity.getProName()+"<br>"+entity.getCusMobile());
                    Object orderNo0 = entity.getOrderNo();
					Object orderNo1 = entity.getOrderNo();
					Object orderNo2 = entity.getOrderNo();
					Object orderNo3 = entity.getOrderNo();
					Object orderNo4 = entity.getOrderNo();
					Object orderNo5 = entity.getOrderNo();
					Object orderNo6 = entity.getOrderNo();
//				    String hql0 = "from FxjOrderLogEntity where 1 = 1 AND orderNo = ? ";
//	    			List<FxjOrderLogEntity> fxjOrderLogOldList = this.fxjOrderMainService.findHql(hql0,orderNo0);
//				    page.setFxjOrderLogList(fxjOrderLogOldList);
//				    String hql1 = "from FxjOrderObjEntity where 1 = 1 AND orderNo = ? ";
//	    			List<FxjOrderObjEntity> fxjOrderObjOldList = this.fxjOrderMainService.findHql(hql1,orderNo1);
//				    page.setFxjOrderObjList(fxjOrderObjOldList);
//				    String hql2 = "from FxjOrderLoanEntity where 1 = 1 AND orderNo = ? ";
//	    			List<FxjOrderLoanEntity> fxjOrderLoanOldList = this.fxjOrderMainService.findHql(hql2,orderNo2);
//				    page.setFxjOrderLoanList(fxjOrderLoanOldList);
//				    try{
//                        page.setCusRemark(fxjOrderLoanOldList.get(0).getElementContent());
//
//                    }catch (Exception e){
//
//                    }
//
//                    String hql3 = "from FxjOrderEmerEntity where 1 = 1 AND orderNo = ? ";
//	    			List<FxjOrderEmerEntity> fxjOrderEmerOldList = this.fxjOrderMainService.findHql(hql3,orderNo3);
//				    page.setFxjOrderEmerList(fxjOrderEmerOldList);
//				    String hql4 = "from FxjOrderDueEntity where 1 = 1 AND orderNo = ? ";
//	    			List<FxjOrderDueEntity> fxjOrderDueOldList = this.fxjOrderMainService.findHql(hql4,orderNo4);
//				    page.setFxjOrderDueList(fxjOrderDueOldList);
//				    String hql5 = "from FxjOrderAttaEntity where 1 = 1  AND bpmStatus = 1 AND orderNo = ? ";
//	    			List<FxjOrderAttaEntity> fxjOrderAttaOldList = this.fxjOrderMainService.findHql(hql5,orderNo5);
//				    page.setFxjOrderAttaList(fxjOrderAttaOldList);
//				    String hql6 = "from FxjOrderRmEntity where 1 = 1 AND orderNo = ? ";
//	    			List<FxjOrderRmEntity> fxjOrderRmOldList = this.fxjOrderMainService.findHql(hql6,orderNo6);
//				    page.setFxjOrderRmList(fxjOrderRmOldList);
            		pageList.add(page);
            	}catch(Exception e){
            		logger.info(e.getMessage());
            	}
            }
        }
		return Result.success(pageList,(long)listsize.size());
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="根据ID获取订单信息",notes="根据ID获取订单信息",httpMethod="GET",produces="application/json")
	public ResponseMessage<?> get(@ApiParam(required=true,name="id",value="ID")@PathVariable("id") String id) {
		FxjOrderMainEntity task = null;
		try{
			task = fxjOrderMainService.get(FxjOrderMainEntity.class, Integer.parseInt(id));

		}catch (Exception e){

		}
		if (task == null) {
			try{
				task = fxjOrderMainService.findUniqueByProperty(FxjOrderMainEntity.class,"orderNo",id);

			}catch (Exception e){

			}
			if (task == null) {
				return Result.error("根据ID获取订单信息为空");
			}
		}
		FxjOrderMainPage page = new FxjOrderMainPage();
		try {
			MyBeanUtils.copyBeanNotNull2Bean(task, page);
				Object orderNo0 = task.getOrderNo();
				Object orderNo1 = task.getOrderNo();
				Object orderNo2 = task.getOrderNo();
				Object orderNo3 = task.getOrderNo();
				Object orderNo4 = task.getOrderNo();
				Object orderNo5 = task.getOrderNo();
				Object orderNo6 = task.getOrderNo();
		    String hql0 = "from FxjOrderLogEntity where 1 = 1 AND orderNo = ? ";
			List<FxjOrderLogEntity> fxjOrderLogOldList = this.fxjOrderMainService.findHql(hql0,orderNo0);
		    page.setFxjOrderLogList(fxjOrderLogOldList);
			
			
		    String hql1 = "from FxjOrderObjEntity where 1 = 1 AND orderNo = ? ";
			List<FxjOrderObjEntity> fxjOrderObjOldList = this.fxjOrderMainService.findHql(hql1,orderNo1);
		    page.setFxjOrderObjList(fxjOrderObjOldList);
			
			
		    String hql2 = "from FxjOrderLoanEntity where 1 = 1 AND orderNo = ? ";
			List<FxjOrderLoanEntity> fxjOrderLoanOldList = this.fxjOrderMainService.findHql(hql2,orderNo2);
		    page.setFxjOrderLoanList(fxjOrderLoanOldList);
			
			
		    String hql3 = "from FxjOrderEmerEntity where 1 = 1 AND orderNo = ? ";
			List<FxjOrderEmerEntity> fxjOrderEmerOldList = this.fxjOrderMainService.findHql(hql3,orderNo3);
		    page.setFxjOrderEmerList(fxjOrderEmerOldList);
			
			
		    String hql4 = "from FxjOrderDueEntity where 1 = 1 AND orderNo = ? ";
			List<FxjOrderDueEntity> fxjOrderDueOldList = this.fxjOrderMainService.findHql(hql4,orderNo4);
		    page.setFxjOrderDueList(fxjOrderDueOldList);
			
			
		    String hql5 = "from FxjOrderAttaEntity where 1 = 1 AND bpmStatus = 1 AND orderNo = ? ";
			List<FxjOrderAttaEntity> fxjOrderAttaOldList = this.fxjOrderMainService.findHql(hql5,orderNo5);
			List<FxjOrderAttaEntity> fxjOrderAttanewList = this.fxjOrderMainService.findHql(hql5,orderNo5);
		    page.setFxjOrderAttaList(fxjOrderAttaOldList);

			//获取参数
			Object proNo0 = page.getProNo();
			//===================================================================================
			//查询-附件
			String hqlpro = "from FxjProAttEntity where 1 = 1 AND proNo = ? ";
			List<FxjProAttEntity> fxjProAttEntityList = systemService.findHql(hqlpro,proNo0);
			List<FxjorderProAttEntity> orderproatt = new ArrayList<>();
			for(FxjProAttEntity tpro: fxjProAttEntityList){
				FxjorderProAttEntity torderpro = new FxjorderProAttEntity();
				MyBeanUtils.copyBeanNotNull2Bean(tpro,torderpro);
				List<FxjOrderAttaEntity> fxjOrderAttadList = new ArrayList<>();

				for(FxjOrderAttaEntity taar: fxjOrderAttaOldList){
					if(tpro.getAttType().equals(taar.getAttaType())){
						fxjOrderAttadList.add(taar);
						fxjOrderAttanewList.remove(taar);
					}
				}

				torderpro.setFxjOrderAttaList(fxjOrderAttadList);
				orderproatt.add(torderpro);
			}
			if(fxjOrderAttanewList.size()>0){
				FxjorderProAttEntity torderpro = new FxjorderProAttEntity();
				torderpro.setAttType("99");
				torderpro.setAttName("其他");
				torderpro.setFxjOrderAttaList(fxjOrderAttanewList);
				orderproatt.add(torderpro);

			}
		    page.setFxjProAttList(orderproatt);
			
		    String hql6 = "from FxjOrderRmEntity where 1 = 1 AND orderNo = ? ";
			List<FxjOrderRmEntity> fxjOrderRmOldList = this.fxjOrderMainService.findHql(hql6,orderNo6);
		    page.setFxjOrderRmList(fxjOrderRmOldList);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.success(page);
	}
 	
// 	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value="/{username}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="创建订单")
	public ResponseMessage<?> create(@PathVariable("username") String username, @ApiParam(name="订单对象")@RequestBody FxjOrderMainPage fxjOrderMainPage, UriComponentsBuilder uriBuilder) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<FxjOrderMainPage>> failures = validator.validate(fxjOrderMainPage);
		String orderNo= "";
		String sysuser="";
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}
		try{
			try{

				TSUser task = fxjUtil.getsysorgcode(username);
				if(task!=null){
                    fxjOrderMainPage.setCreateBy(username);
                    fxjOrderMainPage.setCreateName(task.getRealName());
                    fxjOrderMainPage.setSysOrgCode(task.getCurrentDepart().getSysOrgCode());

                }

				try{
					FxjProMainEntity fxjProMainEntity = systemService.findUniqueByProperty(FxjProMainEntity.class,"proNo",fxjOrderMainPage.getProNo());
					if(fxjProMainEntity!=null){
						fxjOrderMainPage.setProClass(fxjProMainEntity.getProClass());
						fxjOrderMainPage.setProName(fxjProMainEntity.getProName());
						fxjOrderMainPage.setProType(fxjProMainEntity.getProType());
						fxjOrderMainPage.setProWf(fxjProMainEntity.getProWorkflow());
                        fxjOrderMainPage.setProIsdb(fxjProMainEntity.getProIsdb());
						if(StringUtil.isEmpty(fxjOrderMainPage.getBpmStatus()))
						{
							fxjOrderMainPage.setBpmStatus("10");
						}
						if("1".equals(fxjOrderMainPage.getBpmStatus())){
							fxjOrderMainPage.setOrderStatus("草稿");
						}
						if("10".equals(fxjOrderMainPage.getBpmStatus())){
							fxjOrderMainPage.setOrderStatus("已提交");
						}
						try{
							FxjSysWfEntity fxjSysWfEntity =  fxjUtil.getwf(fxjOrderMainPage.getProWf(),fxjOrderMainPage.getBpmStatus());

						if(fxjSysWfEntity!=null){
							fxjOrderMainPage.setMpbackUrl(fxjSysWfEntity.getMpbackUrl());
							fxjOrderMainPage.setMpName(fxjSysWfEntity.getMpName());
							fxjOrderMainPage.setMpUrl(fxjSysWfEntity.getMpUrl());
							fxjOrderMainPage.setWbbackUrl(fxjSysWfEntity.getWbbackUrl());
							fxjOrderMainPage.setWbName(fxjSysWfEntity.getWbName());
							fxjOrderMainPage.setWbUrl(fxjSysWfEntity.getWbUrl());
						}
						}catch (Exception e){

						}

					}
				}catch (Exception e){

				}



			}catch (Exception e){
			}


			FxjOrderLogEntity fxjOrderLogEntity = new FxjOrderLogEntity();
			fxjOrderLogEntity.setOrderNo(fxjOrderMainPage.getOrderNo());
            fxjOrderLogEntity.setLogUser(fxjOrderMainPage.getCreateName());
			fxjOrderLogEntity.setLogDate(now());
			fxjOrderLogEntity.setLogContent("创建订单");
			fxjOrderMainService.save(fxjOrderLogEntity);
			fxjOrderMainPage.setOrderBy18("weifenpei");
			fxjOrderMainService.addMain(fxjOrderMainPage);
		}catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存订单失败");
        }
		return Result.success(orderNo);
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="更新订单",notes="更新订单")
	public ResponseMessage<?> update(@RequestBody FxjOrderMainPage fxjOrderMainPage) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.

		Set<ConstraintViolation<FxjOrderMainPage>> failures = validator.validate(fxjOrderMainPage);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}
		try {
//			List<FxjOrderLogEntity> fxjOrderLogList = fxjOrderMainPage.getFxjOrderLogList();
			FxjOrderLogEntity fxjOrderLogEntity = new FxjOrderLogEntity();
			fxjOrderLogEntity.setOrderNo(fxjOrderMainPage.getOrderNo());
			fxjOrderLogEntity.setLogUser(fxjOrderMainPage.getUpdateBy());
            try{
                TSUser task = fxjUtil.getsysorgcode(fxjOrderMainPage.getUpdateBy());
                if (task!=null){
                    fxjOrderLogEntity.setLogUser(task.getRealName());
                }

            }catch (Exception e){

            }
			fxjOrderLogEntity.setLogDate(now());
			FxjSysWfEntity fxjSysWfEntity =  fxjUtil.genwf(fxjOrderMainPage.getOrderNo(),fxjOrderMainPage.getOrderOper());
			if(fxjSysWfEntity!=null){
				fxjOrderMainPage.setMpbackUrl(fxjSysWfEntity.getMpbackUrl());
				fxjOrderMainPage.setMpName(fxjSysWfEntity.getMpName());
				fxjOrderMainPage.setMpUrl(fxjSysWfEntity.getMpUrl());
				fxjOrderMainPage.setWbbackUrl(fxjSysWfEntity.getWbbackUrl());
				fxjOrderMainPage.setWbName(fxjSysWfEntity.getWbName());
				fxjOrderMainPage.setWbUrl(fxjSysWfEntity.getWbUrl());
				fxjOrderMainPage.setBpmStatus(fxjSysWfEntity.getWfNowStep());
				fxjOrderMainPage.setOrderStatus(fxjSysWfEntity.getWfComment());
				fxjOrderLogEntity.setBpmStatus(fxjSysWfEntity.getWfNowStep()+fxjSysWfEntity.getWfComment());
			}
			try{
				fxjOrderLogEntity.setLogContent(fxjSysWfEntity.getWfComment()+":"+fxjOrderMainPage.getFxjOrderLogList().get(0).getLogContent());//审批意见
			}catch (Exception e){

			}


			systemService.save(fxjOrderLogEntity);
			fxjOrderMainPage.setFxjOrderLogList(null);
            fxjOrderMainPage.setUpdateDate(now());
			fxjOrderMainService.updateMain(fxjOrderMainPage);
			try{
				fxjUtil.changeBpmstatus(fxjOrderMainPage.getOrderNo(),fxjOrderMainPage.getBpmStatus());

			}catch (Exception e){

			}
		} catch (Exception e) {
			logger.info(e.getMessage());
	        return Result.error("更新订单失败");
		}
		//按Restful约定，返回204状态码, 无内容. 也可以返回200状态码.
		return Result.success();
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ApiOperation(value="删除订单")
	public ResponseMessage<?> delete(@ApiParam(name="id",value="ID",required=true)@PathVariable("id") String id) {
		logger.info("delete[{}]" + id);
		// 验证
		if (StringUtils.isEmpty(id)) {
			return Result.error("ID不能为空");
		}
		try {
			FxjOrderMainEntity fxjOrderMain = fxjOrderMainService.get(FxjOrderMainEntity.class, id);
			fxjOrderMainService.delMain(fxjOrderMain);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("订单删除失败");
		}

		return Result.success();
	}
	
}
